<template>
    <div>
        <!-- 顶部可滑动导航-->
        <div id="slider" class="mui-slider">
				<div id="sliderSegmentedControl" class="mui-scroll-wrapper mui-slider-indicator mui-segmented-control mui-segmented-control-inverted">
					<div class="mui-scroll">
                        <!-- 动态绑定class,循环的时候，如果id为0，则添加active类名 -->
                        <!--mui @click绑定事件兼容性有问题，换成 @tap-->
						<a :class="['mui-control-item', item.id === 0? 'mui-active':'']" v-for="item in cates" :key="item.id" @tap="getImgById(item.id)">
							{{item.title}}
						</a>
					</div>
				</div>

			</div>
            <ul class="photo-list">
                <!-- tag指定路由链接用li标签渲染 -->
                <router-link v-for="item in list" :key="item.id" tag="li" :to="'/home/photoinfo/'+item.id">
                    <img v-lazy="item.img_url">
                    <div class="info">
                        <h3>{{item.title}}</h3>
                        <p>{{item.zhaiyao}}</p>
                    </div>
                </router-link>
            </ul>
    </div>
    
</template>
<script>
import mui from '../../lib/mui/js/mui.min.js'
import {Toast} from 'mint-ui'
export default {
    data(){
        return{
            cates:[],//所有分类
            list:[],//图片数组
        }
    },
    mounted(){//当组件中的dom节点加载完成时候
    //如果要操作dom最好都写在mounted钩子函数里，这时候的dom是最新渲染完成的
        mui('.mui-scroll-wrapper').scroll();
    },
    created(){
        this.getAllCategory()
        this.getImgById(0)
    },
    methods:{
        //接口名字getimgcategory
        getAllCategory(){
            this.$http.get('getimgcategory').then(res => {
                if( res.body.status === 0 ){ 
                    //由于没有全部这个分类，需要自己手动拼接
                    res.body.message.unshift({ title:'全部', id: 0 })//先追加，后赋值，unshift方法返回值是新数组的长度，不能直接赋值给cates
                    this.cates = res.body.message
                    
                }else{
                    Toast('分类获取失败')
                }
            })
        },
        getImgById(cateId){
            this.$http.get('getimages/'+cateId).then(res => {
                if(res.body.status === 0){
                    this.list = res.body.message
                }
            })
        }
    }
}
</script>
<style lang="scss" scoped>
*{
    touch-action: pan-x;
}
.mui-scroll-wrapper{
    overflow: scroll;
    height: auto;
}
.photo-list{
    list-style: none;
    padding: 0 10px;
    margin: 0;
    padding-bottom: 0px;
    li{
        background: #ccc;
        margin-bottom: 10px;
        text-align: center;
        box-shadow: 0 0 9px #f7f7f7;
        position: relative;
        img{
            width: 100%;
            vertical-align: middle;
        }
        img[lazy=loading] {
        width: 40px;
        height: 300px;
        margin: auto;
        }
        .info{
            text-align: left;
            color:#fff;
            position: absolute;
            bottom: 0;
            padding:10px;
            max-height: 80px;
            text-overflow: ellipsis;
            overflow: hidden;
            background-color: rgba(0,0,0,0.5);
            h3{
                font-size: 14px;
            }
            p{
                font-size: 13px;
            }
        }
    }
}

</style>